package com.ctshk.app.order.season.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.enums.SeasonType;
import com.ctshk.common.model.AppReq;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.req.pay.NotifyConsumeReq;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.season.dto.SeasonOrderPayDTO;
import com.ctshk.rpc.order.season.dto.SeasonOrderRefundDTO;
import com.ctshk.rpc.order.season.dto.SeasonProductReplenishDTO;
import com.ctshk.rpc.order.season.dto.SeasonSearchOrderDTO;
import com.ctshk.rpc.order.season.req.*;
import com.ctshk.rpc.order.season.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 套票订单 前端控制器
 * </p>
 *
 * @author 杨林
 * @since 2021-03-24
 */
@Api(value = "APP套票-订单中心", tags = "APP套票-订单中心")
@RestController
@RequestMapping("/appSeasonOrder")
public class H5SeasonOrderController extends BaseController {

    @DubboReference
    private ISeasonOrderCollectionService collectionService;
    @DubboReference
    private ISeasonOrderService seasonOrderService;
    @DubboReference
    private ISeasonOrderRefundService orderRefundService;
    @DubboReference
    private ISeasonReceiptService receiptService;
    @DubboReference
    private ISeasonOrderReceiptService seasonOrderReceiptService;

//    @ApiOperation(value = "APP套票-订单中心-唤起支付", notes = "APP套票-订单中心-唤起支付")
//    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "APP套票-订单中心-唤起支付", operDesc = "APP套票-订单中心-唤起支付")
//    @PostMapping("/callPay")
//    public void callPay(@RequestBody @Valid SeasonPortExpressOrderAppCallPayReq callPayReq, HttpServletResponse response) {
//        Result result = seasonOrderService.callPay(callPayReq);
//        response.setContentType("text/html");
//        response.setCharacterEncoding("UTF-8");
//        try {
//            response.getWriter().write(String.valueOf(result.getData()));
//            response.getWriter().flush();
//            response.getWriter().close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

//    @ApiOperation(value = "APP套票-订单中心-支付订单(支付模块调用)", notes = "APP套票-订单中心-支付订单(支付模块调用)")
//    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "APP套票-订单中心-支付订单", operDesc = "APP套票-订单中心-支付订单")
//    @PostMapping("/payment")
//    public Result payment(@RequestBody @Valid NotifyConsumeReq notifyConsumeReq) {
//        return seasonOrderService.payment(notifyConsumeReq, getCurrentUserAllowNotLogInUserId());
//    }

    @ApiOperation(value = "预订中心-套票-计算价格", notes = "预订中心-套票-计算价格")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "固定套票-计算价格", operDesc = "固定套票-计算价格")
    @PostMapping("/calculatePrice")
    public Result calculatePrice(@RequestBody @Valid AppCalculatePriceReq req, @ApiIgnore AppReq appReq) {
        return Result.success(seasonOrderService.getPackageProduct(req, SeasonType.ONLINE_SELF_CHANNEL_TYPE.getCode(), appReq.getChannelId()));
    }

    @ApiOperation(value = "APP订单中心-套票-订单收据详情", notes = "APP订单中心-套票-订单收据详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "APP订单中心-套票-订单收据", operDesc = "APP订单中心-套票-订单收据")
    @PostMapping(value = "/receiptInfo/{id}")
    public Result receiptInfo(@PathVariable("id") Long id) {
        TokenUser user = getCurrentUser(getRequest());
        return seasonOrderReceiptService.getReceiptInfo(id, user);
    }

    @ApiOperation(value = "APP订单中心-套票-编辑订单收据", notes = "APP订单中心-套票-编辑订单收据")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "APP订单中心-套票-编辑订单收据", operDesc = "APP订单中心-套票-编辑订单收据")
    @PostMapping(value = "/receiptEdit")
    public Result editReceipt(@RequestBody SeasonOrderReceiptReq req) {
        TokenUser user = getCurrentUser(getRequest());
        return seasonOrderReceiptService.editReceipt(req, user);
    }

    @ApiOperation(value = "APP订单中心-套票-收款记录", notes = "APP订单中心-套票-收款记录列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "APP订单中心-套票-收款记录列表", operDesc = "APP订单中心-套票-收款记录列表")
    @PostMapping("/collectionList/{id}")
    public Result SeasonOrderCollectionList(@PathVariable Long id) {
        List<SeasonOrderPayDTO> collectionList = collectionService.getCollectionListById(id);
        return Result.success(collectionList);
    }


    @ApiOperation(value = "APP订单中心-套票-收款再次确认操作", notes = "APP订单中心-套票-收款再次确认")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "APP订单中心-套票-收款再次确认", operDesc = "APP订单中心-套票-收款再次确认")
    @PostMapping("/reconfirmCollection/{id}")
    public Result reconfirmCollection(@PathVariable Long id) {
        return collectionService.reconfirmCollection(id);
    }

    @ApiOperation(value = "预订中心-套票-填写订单信息", notes = "预订中心-套票-填写订单信息")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "固定套票-填写订单信息", operDesc = "固定套票-填写订单信息")
    @PostMapping("/save")
    public Result list(@RequestBody @Valid SeasonOrderReq req,@ApiIgnore AppReq appReq) {

        req.setChannelId(appReq.getChannelId());
        return seasonOrderService.saveSeasonOrder(req, getCurrentUserAllowNotLogInUserId(), SeasonType.ONLINE_SELF_CHANNEL_TYPE.getCode(),appReq);
    }


    @ApiOperation(value = "预订中心-套票-核对订单信息", notes = "预订中心-套票-核对订单信息")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "套票-订单-核对订单信息", operDesc = "套票-订单-核对订单信息")
    @PostMapping("/checkSeasonOrder")
   public Result checkSeasonOrder(@RequestBody @Valid SeasonOrderReq req) {
        TokenUser user = getCurrentUser(getRequest());
        return seasonOrderService.checkSeasonOrder(req, user);

    }

    @ApiOperation(value = "预订中心-套票-订单收款", notes = "预订中心-套票-订单收款")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "套票-订单-订单收款", operDesc = "套票-订单-订单收款")
    @PostMapping("/payOrder")
    public Result payOrder(@RequestBody @Valid List<SeasonOrderPayReq> reqList) {
        return seasonOrderService.saveOrderPay(reqList, getCurrentUserAllowNotLogInUserId());
    }

    @ApiOperation(value = "APP订单中心-套票-套票订单搜索", notes = "APP订单中心-套票-套票订单搜索")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "APP订单中心-套票-套票订单搜索", operDesc = "APP订单中心-套票-套票订单搜索")
    @PostMapping("/searchOrder")
    public Result searchSeasonOrder(@RequestBody @Valid SearchSeasonOrderReq req) {
        TokenUser user = getCurrentUser(getRequest());
        return seasonOrderService.searchSeasonOrder(req, user);
    }

    @ApiOperation(value = "APP订单中心-套票-订单详情", notes = "APP订单中心-套票-订单详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "APP订单中心-套票-订单详情", operDesc = "APP订单中心-套票-套票订单详情")
    @PostMapping(value = "/detailsInfo/{id}")
    public Result<SeasonSearchOrderDTO> detailsInfo(@PathVariable Long id) {
        SeasonSearchOrderDTO detailsDTO = seasonOrderService.getOrderInfoById(id);
        return Result.success(detailsDTO);
    }

    @ApiOperation(value = "APP订单中心-套票-未付款订单取消订单", notes = "APP订单中心-套票-未付款订单取消订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "APP订单中心-套票-未付款订单取消订单", operDesc = "APP订单中心-套票-未付款订单取消订单")
    @PostMapping(value = "/cancelOrder")
    public Result cancelOrder(@RequestBody @Valid SeasonOrderCancelReq req) {
        return seasonOrderService.cancelOrder(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "APP订单中心-套票-取消订单/订单退款", notes = "APP订单中心-套票-取消订单/订单退款")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "APP订单中心-套票-取消订单/订单退款", operDesc = "APP订单中心-套票-取消订单/订单退款")
    @PostMapping(value = "/refundOrder")
    public Result refundOrder(@RequestBody @Valid SeasonOrderRefundReq req) {
        return seasonOrderService.refundOrder(req, getCurrentUser(getRequest()));
    }


    @ApiOperation(value = "APP订单中心-套票-预订须知", notes = "APP订单中心-套票-预订须知")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "APP订单中心-套票-预订须知", operDesc = "APP订单中心-套票-预订须知")
    @PostMapping(value = "/replenishInfo/{productId}")
    public Result productReplenishInfo(@PathVariable Long productId) {
        SeasonProductReplenishDTO dto = seasonOrderService.getProductReplenishInfo(productId);
        return Result.success(dto);
    }


    @ApiOperation(value = "APP订单中心-套票-退款审批", notes = "APP订单中心-套票-退款审批")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "APP订单中心-套票-退款审批", operDesc = "APP订单中心-套票-退款审批")
    @PostMapping(value = "/refundAuditResult")
    public Result refundAuditResult(@RequestBody @Valid RefundAuditReq req) {
        TokenUser user = getCurrentUser(getRequest());
        return seasonOrderService.refundAuditResult(req, user);
    }


    @ApiOperation(value = "通知旅客", notes = "通知旅客", tags = "套票-通知旅客")
    @OperationLogAnnotation(operModul = OperationLogModel.VISA, operType = OperationLogType.ADD, operTitle = "套票-通知旅客", operDesc = "套票-通知旅客")
    @PostMapping("/notice")
    public Result notice(@RequestBody @Valid SeasonOrderNoticeReq seasonOrderNoticeReq) {
        return seasonOrderService.notice(seasonOrderNoticeReq);
    }

    @ApiOperation(value = "预订中心-自由行套票-购物车列表", notes = "预订中心-自由行套票-购物车列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SEASONFIXEDPRODUCT, operType = OperationLogType.QUERY, operTitle = "自由行套票购物车列表", operDesc = "自由行套票购物车列表")
    @PostMapping("/cartList")
    public Result list() {
        TokenUser user = getCurrentUser(getRequest());
        return receiptService.getCartList(user);
    }


    @ApiOperation(value = "预订中心-自由行套票-添加产品到购物车", notes = "预订中心-自由行套票-添加产品到购物车")
    @OperationLogAnnotation(operModul = OperationLogModel.SEASONFIXEDPRODUCT, operType = OperationLogType.ADD, operTitle = "添加产品到购物车", operDesc = "添加产品到购物车")
    @PostMapping("/addProduct")
    public Result addProductCart(@RequestBody DynamicProductReq req) {
        TokenUser user = getCurrentUser(getRequest());
        return receiptService.addProductCart(user, req);
    }

    @ApiOperation(value = "预订中心-自由行套票-删除产品", notes = "预订中心-自由行套票-删除产品")
    @OperationLogAnnotation(operModul = OperationLogModel.SEASONFIXEDPRODUCT, operType = OperationLogType.DELETE, operTitle = "删除产品", operDesc = "删除产品")
    @PostMapping("/deleteProduct")
    public Result deleteProductCart(@RequestBody DeleteDynamicProductReq req) {
        TokenUser user = getCurrentUser(getRequest());
        return receiptService.deleteProductCart(user, req);
    }

    @ApiOperation(value = "预订中心-自由行套票-批量删除产品", notes = "预订中心-自由行套票-批量删除产品")
    @OperationLogAnnotation(operModul = OperationLogModel.SEASONFIXEDPRODUCT, operType = OperationLogType.DELETE, operTitle = "删除产品", operDesc = "删除产品")
    @PostMapping("/deleteBatchProductCart")
    public Result deleteBatchProductCart(@RequestBody List<DeleteDynamicProductReq> reqs) {
        TokenUser user = getCurrentUser(getRequest());
        return receiptService.deleteBatchProductCart(user, reqs);
    }

    @ApiOperation(value = "APP订单中心-套票-退款记录", notes = "APP订单中心-套票-退款记录列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "APP订单中心-套票-退款记录列表", operDesc = "APP订单中心-套票-退款记录列表")
    @PostMapping("/orderRefundList/{id}")
    public Result findOrderRefundList(@PathVariable Long id) {
        List<SeasonOrderRefundDTO> touristList = orderRefundService.getOrderRefundByOrderId(id);
        return Result.success(touristList);

    }


    @ApiOperation(value = "APP订单中心-套票-现金操作退款", notes = "APP订单中心-套票-现金操作退款")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "APP订单中心-套票-现金操作退款", operDesc = "APP订单中心-套票-现金操作退款")
    @PostMapping("/refundCash/{id}")
    public Result refundCash(@PathVariable Long id) {
        TokenUser user = getCurrentUser(getRequest());
        return orderRefundService.refundCash(id, user);
    }
}
